package com.lemon.components.wechart.impl;

import java.util.Iterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lemon.components.wechart.SupporterService;
import com.lemon.components.wechart.WechartBase;
import com.lemon.components.wechart.order.supporter.AddSupporterOrder;
import com.lemon.components.wechart.order.supporter.DelSupporterOrder;
import com.lemon.components.wechart.order.supporter.GetkfListOrder;
import com.lemon.components.wechart.order.supporter.SendMsgOrder;
import com.lemon.components.wechart.order.supporter.SendTextMsgOrder;
import com.lemon.components.wechart.result.supporter.AddSupporterResult;
import com.lemon.components.wechart.result.supporter.DelSupporterResult;
import com.lemon.components.wechart.result.supporter.GetkfListResult;
import com.lemon.components.wechart.result.supporter.SendMsgResult;
import com.lemon.components.wechart.result.supporter.SupporterInfo;

/**
 * 客服服务
 * @author jlcon
 *
 */
public class SupporterServiceImpl extends WechartBase implements SupporterService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private final String ADDSUPPORTER="https://api.weixin.qq.com/customservice/kfaccount/add?access_token=%1$s";
	private final String DELSUPPORTER="https://api.weixin.qq.com/customservice/kfaccount/del?access_token=%1$s";
	private final String GETKFLISTURL="https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=%1$s";
	private final String SENDMSGURL="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%1$s";
	
	/* (non-Javadoc)
	 * @see com.yjf.esupplier.service.wechart.impl.SupporterService#addSupporter(com.yjf.esupplier.service.wechart.order.supporter.AddSupporterOrder)
	 */
	@Override
	public AddSupporterResult addSupporter(AddSupporterOrder addSupporterOrder){
		addSupporterOrder.check();
		AddSupporterResult addSupporterResult = new AddSupporterResult();
		JSONObject temp = (JSONObject) JSONObject.toJSON(addSupporterOrder);
		temp.remove("checkGid");
		temp.remove("accessToken");
		String str = postCommand(String.format(ADDSUPPORTER, addSupporterOrder.getAccessToken()),temp.toJSONString());
		JSONObject jsonObject = JSONObject.parseObject(str);
		int errcode = jsonObject.getIntValue("errcode");
		if (errcode > 0) {
			addSupporterResult.setErrcode(errcode);
			addSupporterResult.setErrmsg(jsonObject.getString("errmsg"));
			return addSupporterResult;
		} else {
			addSupporterResult.setErrcode(errcode);
			addSupporterResult.setErrmsg(jsonObject.getString("errmsg"));
			return addSupporterResult;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.yjf.esupplier.service.wechart.impl.SupporterService#getkflist(com.yjf.esupplier.service.wechart.order.supporter.GetkfListOrder)
	 */
	@Override
	public GetkfListResult getkflist(GetkfListOrder getkfListOrder){
		getkfListOrder.check();
		GetkfListResult getkfListResult = new GetkfListResult();
		String str = getCommand(String.format(GETKFLISTURL, getkfListOrder.getAccessToken()));
		JSONObject jsonObject = JSONObject.parseObject(str);
		int errcode = jsonObject.getIntValue("errcode");
		if (errcode > 0) {
			getkfListResult.setErrcode(errcode);
			getkfListResult.setErrmsg(jsonObject.getString("errmsg"));
			return getkfListResult;
		} else {
			JSONArray jsonArray = jsonObject.getJSONArray("kf_list");
			for(Iterator<Object> iter = jsonArray.iterator();iter.hasNext();){
				JSONObject kfinfo = (JSONObject)iter.next();
				SupporterInfo supporterInfo = new SupporterInfo();
				supporterInfo.setKfAccount(kfinfo.getString("kf_account"));
				supporterInfo.setKfNick(kfinfo.getString("kf_nick"));
				supporterInfo.setKfId(kfinfo.getString("kf_id"));
				supporterInfo.setKfHeadimgurl(kfinfo.getString("kf_headimgurl"));
				getkfListResult.getKfList().add(supporterInfo);
			}
		}
		return getkfListResult;
	}
	
	/* (non-Javadoc)
	 * @see com.yjf.esupplier.service.wechart.impl.SupporterService#delSupporter(com.yjf.esupplier.service.wechart.order.supporter.DelSupporterOrder)
	 */
	@Override
	public DelSupporterResult delSupporter(DelSupporterOrder delSupporterOrder){
		delSupporterOrder.check();
		DelSupporterResult delSupporterResult = new DelSupporterResult();
		String str = postCommand(String.format(DELSUPPORTER, delSupporterOrder.getAccessToken()),JSONObject.toJSONString(delSupporterOrder));
		JSONObject jsonObject = JSONObject.parseObject(str);
		int errcode = jsonObject.getIntValue("errcode");
		if (errcode > 0) {
			delSupporterResult.setErrcode(errcode);
			delSupporterResult.setErrmsg(jsonObject.getString("errmsg"));
			return delSupporterResult;
		} else {
			delSupporterResult.setErrcode(errcode);
			delSupporterResult.setErrmsg(jsonObject.getString("errmsg"));
			return delSupporterResult;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.yjf.esupplier.service.wechart.impl.SupporterService#sendMsg(com.yjf.esupplier.service.wechart.order.supporter.SendMsgOrder)
	 */
	@Override
	public SendMsgResult sendMsg(SendMsgOrder sendMsgOrder){
		SendMsgResult rendMsgResult = new SendMsgResult();
		String commonurl = String.format(SENDMSGURL, sendMsgOrder.getAccessToken());
		String jsoncommond = null;
		if(sendMsgOrder instanceof SendTextMsgOrder){
			SendTextMsgOrder sendTextMsgOrder = (SendTextMsgOrder)sendMsgOrder;
			sendTextMsgOrder.check();
			jsoncommond = JSONObject.toJSONString(sendTextMsgOrder);
		} else {
			rendMsgResult.setErrcode(-3);
			rendMsgResult.setErrmsg("未找到相应操作");
			return rendMsgResult;
		}
		String str = postCommand(commonurl,jsoncommond);
		JSONObject jsonObject = JSONObject.parseObject(str);
		int errcode = jsonObject.getIntValue("errcode");
		if (errcode > 0) {
			rendMsgResult.setErrcode(errcode);
			rendMsgResult.setErrmsg(jsonObject.getString("errmsg"));
			return rendMsgResult;
		} else {
			rendMsgResult.setErrcode(errcode);
			rendMsgResult.setErrmsg(jsonObject.getString("errmsg"));
			return rendMsgResult;
		}
	}
}
